/* beautify ignore:start */
import {provide, Directive} from '@angular/core';
import {Control, NG_VALIDATORS, Validator} from '@angular/common';
/* beautify ignore:end */

@Directive({
    selector: '[ip]',
    providers: [provide(NG_VALIDATORS, {useExisting: IPValidator, multi: true})],
})

export class IPValidator implements Validator {
    validate(c: Control): {[key: string]: any} {
        var ipRegexp = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/  ;
        var ipInnerRegexp = /^(10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|172\.16\.\d{1,3}\.\d{1,3})$/;
        if (c.value && !ipRegexp.test(c.value)) {
                 return {'ip': true};
        }
        if(ipInnerRegexp.test(c.value)) {
            return {'ipInner': true};    
        }
    }
}
